(function() {
    var myckeditor;
    require(["ckeditor"], function(ckeditor) {
        if (!myckeditor) {
           myckeditor = CKEDITOR.replace("ckeditor");
        };
    });
    Path.on("/main/__basename__.html", function(_current_page) {
        // 获取板块
        coAjax.get(public_api.section.get.list_all, function(result) {
            App.set("$Cache.main.article.section_list", result);
            alert("success", "获取板块列表成功");
            // console.log(result);
        });
    });
    // 选择板块
    App.set("$Event.main.article.modulechange", function() {
        if (this.value == "addmodule") {
            // 开启添加板块
            App.set("$Cahce.main.article.openaddmodule", true);
        };
    });
    // 添加新板块
    App.set("$Event.main.article.createmodule", function() {
        var data = App.get("$Cache.main.article.newmodule");
        coAjax.post(public_api.section.create, data, function(result) {
            // console.log(result);
            App.unshift("$Cache.main.article.section_list", result);
            App.set("$Cache.main.article.article.section", result._id);
            alert("success", "板块添加成功");
            App.set("$Cahce.main.article.openaddmodule", false);
        });
    });
    // 取消新增模块
    App.set("$Event.main.article.canceladdmodule", function() {
        App.set("$Cahce.main.article.openaddmodule", false);
        App.set("$Cache.main.article.article.section", "");
    });
    // 添加封面
    App.set("$Event.main.article.addViewPort", function() {
        var file = this.files[0];
        if (file) {
            var upload_id_photo_dtd = new $.Deferred();
            App.set("$Cache.main.article.viewport.isadding", true);
            $.when(upload_image(file, upload_id_photo_dtd)).then(function(image_url) {
                App.set("$Cache.main.article.article.imgurl", image_url);
                App.set("$Cache.main.article.viewport.isadding", false);
            });
        };
    });
    // 删除封面
    App.set("$Event.main.article.delete_viewport", function() {
        App.set("$Cache.main.article.article.imgurl", "");
    });
    // 发布文章
    App.set("$Event.main.article.addarticle", function() {
        // console.log(myckeditor.document.getBody())
        var data = App.get("$Cache.main.article.article") || {};
        // data.html = myckeditor.document.getBody().getHtml();
        data.html=encodeURIComponent(myckeditor.getData());
        // console.log(myckeditor.getData())
        console.log(data);
        if (data == {}) {
            alert("warn", "请先完善文章内容");
            return;
        };
        if (!data.title) {
            alert("warn", "请先填写文章标题！");
            return;
        };
        if (!data.author) {
            alert("warn", "请先填写文章作者!");
            return;
        };
        if (data.html == "<p><br></p>") {
            alert("warn", "请填写文章内容！！");
            return;
        };
        if (data.section == "none") {
            data.section = "";
        };
        coAjax.post(public_api.news.create, data, function(result) {
            console.log(result);
            alert("success", "文章发表成功");
            App.set("$Cache.main.article.article", {});
        });
    });
}());
